package map;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import map.terrain.*;

public class MapBuilder {
	
	private static final MapBuilder instance = new MapBuilder();
	
	private MapBuilder() {
		
	}
	
	public static MapBuilder getInstance() {
		return instance;
	}
	
	public GameMap buildMap(String fileName) {
		
		File file = new File(fileName);
		try {
			Scanner scanner = new Scanner(file);
			
			String mapName = scanner.next();
			int mapWidth = scanner.nextInt();
			int mapHeight = scanner.nextInt();
			
			GameMap toReturn = new GameMap(mapName, mapWidth, mapHeight);
			
			int line = 0;
			while(line < mapHeight) {
				for(int col = 0; col < mapWidth; col++) {
					char tType = scanner.next().charAt(0);
					
					Terrain t = null;
					switch(tType) {	
					
					case 'g':
						t = new Grass();
						break;
					case 'm':
						t = new Mountain();
						break;
					case 'w':
						t = new Water();
						break;
					case 'o':
						t = new Obsidian();
						break;
					case 'l':
						t = new Lava();
						break;
					case 'a':
						t = new Marble();
						break;
					case 'd':
						t = new Dirt();
						break;
					default:
						t = null;
					}
					toReturn.addTerrain(t, new Position(col, line));
				}
				line++;
			}
			
			return toReturn;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
